﻿using Common.Contract;
using Common.Contract.Customer;
using Common.Contract.Customer.Request;
using Common.Contract.SaleOut;
using Common.Contract.SaleOut.Request;
using Common.Contract.Warranty.Record;
using Common.Contract.Warranty.Request;
using Common.Contract.Warranty.Response;
using Common.Service;
using Common.Service.Command;
using Common.Util;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Warranty.Configuration;
using Warranty.Model.Interface;
using Warranty.Model.Model;

namespace Warranty.ServiceCommand
{
    public class SearchWarrantyHistoryServiceCommand : ProviderServiceCommand<IProviderFactory, SearchWarrantyHistoryRequest, SearchWarrantyHistoryResponse>
    {
        public SearchWarrantyHistoryServiceCommand(IProviderFactory provider) : base(provider) { }

        protected override void Validate(SearchWarrantyHistoryRequest request)
        {
            if (request == null) throw new ArgumentNullException("request");
            if (!request.Header.ApplicationKey.HasValue || request.Header.ApplicationKey == Guid.Empty)
            {
                throw new ArgumentException("ApplicationKey must be set", "request");
            }
        }

        protected override bool OnExecute(SearchWarrantyHistoryResponse response)
        {
            bool isSuccess = true;
            string errorMessage = string.Empty;
            var wtHists = Providers.WarrantyHistory.SearchWarranty(Request.SerialNumber, Request.Model, Request.TransferFlag);
            if (wtHists == null || wtHists.Count == 0)
            {
                response.ErrorMessage = string.Format("Can not found PndHistory with Model={0}, SerialNo={1}", Request.Model, Request.SerialNumber);
                response.IsSuccess = false;
                return false;
            }

            var records = wtHists.Select((wtHist) =>
            {
                if (wtHist == null)
                {
                    return null;
                }
                return new WarrantyHistoryRecord
                    {
                        Cmdcode = wtHist.Cmdcode,
                        CreateDate = wtHist.CreateDate,
                        EdiFile = wtHist.EdiFile,
                        EdiHead = wtHist.EdiHead,
                        EndUserCell = wtHist.EndUserCell,
                        Messagebody = wtHist.MoMsgbody,
                        Model = wtHist.Model,
                        Moseq = wtHist.Moseq,
                        ReceiveDate = wtHist.ReceiveDate,
                        ResponseMessage = wtHist.RespMsg,
                        ResponseType = wtHist.RespType,
                        SerialNumber = wtHist.SerialNo,
                        ShopCell = wtHist.ShopCell,
                        ShopCode = wtHist.ShopCode,
                        SmsYn = wtHist.SmsYn.ToBoolean(),
                        TransferFlag = wtHist.SoTransferFlag.ToBoolean()
                    };
            }).ToList();
            response.Records = records;
            return isSuccess;
        }
    }
}
